﻿$(document).ready(function () {
    $.ajax({
        type: "post",
        data: { action: "GetList" },
        datatype: "json",
        url: "../../Ajax/NoticeTypeAjax.ashx",
        beforSend: function () {
        },
        success: function (data) {
            if (data != null) {
                var json = $.parseJSON(data).json;
                var tbody = "";
                $.each(json, function (k, v) {
                    tbody += "<tr>"
                    tbody += "<td><lable name='lblName' id='" + v.ID + "'>";
                    tbody += v.Name;
                    tbody += "</lable></td>";
                    tbody += "<td><lable name='lblIsFullScreen'>";
                    tbody += v.IsFullScreen;
                    tbody += "</lable></td>";
                    tbody += "<td><lable name='lblIsStick'>";
                    tbody += v.IsStick;
                    tbody += "</lable></td>";
                    tbody += "<td><lable name='lblIsRoll'>";
                    tbody += v.IsRoll;
                    tbody += "</lable></td>";
                    tbody += "<td>";
                    tbody += "<input type='button' onclick='Update(this)' value='更新'><input type='button' onclick='Delete(this)' value='删除'>"
                    tbody += "</td>";
                    tbody += "</tr>"
                })
                $("#tb1").empty().append(tbody)
            }
        },
        complete: function () {
        },
        error: function () {
        }
    });
    $("#btnAdd").click(function () {
        Edit(null);
    })
})
function Update(contr) {
    var lbl = $(contr).parent().parent().find("lable[name=lblName]").first();
    Edit(lbl);
}
function Edit(lbl) {
    //lbl 是判断是添加还是修改  添加为null
    var sortNum = $(lbl);
    var Id = $(lbl).parent().parent().find("lable[name=lblName]").attr("id");
    var IsFullScre = $(lbl).parent().parent().find("lable[name=lblIsFullScreen]").text(); //是否全屏
    var IsStick = $(lbl).parent().parent().find("lable[name=lblIsStick]").text(); //是否置顶
    var IsRoll = $(lbl).parent().parent().find("lable[name=lblIsRoll]").text(); //是否滚动
    var data = new Date();
    var content = "";
    if (lbl != null) {
        content = "<table><tr><td>公告类型名称：<input type='text' id='txtName' style='width:200px' value='" + $(lbl).parent().parent().find("lable[name=lblName]").text() + "' /></td></tr>"
        if (IsFullScre == "True") {
            content += "<tr><td>是否全屏：<input type='radio' name='radioIsFullScreen' id='radioisTure' value='True' checked='checked' /><lable>是</lable><input type='radio' name='radioIsFullScreen' id='radioisfalse' value='False'/><lable>否</lable></td></tr>"
        }
        else {
            content += "<tr><td>是否全屏：<input type='radio' name='radioIsFullScreen' id='radioisTure' value='True' /><lable>是</lable><input type='radio' name='radioIsFullScreen' id='radioisfalse' value='False' checked='checked'/><lable>否</lable></td></tr>"
        }
        if (IsStick == "True") {
            content += "<tr><td>是否置顶：<input type='radio' name='radioIsStick' id='radioisTure' value='True' checked='checked' /><lable>是</lable><input type='radio' name='radioIsStick' id='radioisfalse' value='False'/><lable>否</lable></td></tr>"
        }
        else {
            content += "<tr><td>是否置顶：<input type='radio' name='radioIsStick' id='radioisTure' value='True' /><lable>是</lable><input type='radio' name='radioIsStick' id='radioisfalse' value='False' checked='checked' /><lable>否</lable></td></tr>"
        }
        if (IsRoll == "True") {
            content += "<tr><td>是否滚动：<input type='radio' name='radioIsRoll' id='radioisTure' value='True' checked='checked' /><lable>是</lable><input type='radio' name='radioIsRoll' id='radioisfalse' value='False'/><lable>否</lable></td></tr></table>"
        }
        else {
            content += "<tr><td>是否滚动：<input type='radio' name='radioIsRoll' id='radioisTure' value='True' /><lable>是</lable><input type='radio' name='radioIsRoll' id='radioisfalse' value='False' checked='checked'/><lable>否</lable></td></tr></table>"
        }
    }
    else {
        content = "<table><tr><td>公告类型名称：<input type='text' id='txtName' style='width:200px' /></td></tr>"
        content += "<tr><td>是否全屏：<input type='radio' name='radioIsFullScreen' id='radioisTure' value='True' checked='checked' /><lable>是</lable><input type='radio' name='radioIsFullScreen' id='radioisfalse' value='False'/><lable>否</lable></td></tr>"
        content += "<tr><td>是否置顶：<input type='radio' name='radioIsStick' id='radioisTure' value='True' checked='checked' /><lable>是</lable><input type='radio' name='radioIsStick' id='radioisfalse' value='False'/><lable>否</lable></td></tr>"
        content += "<tr><td>是否滚动：<input type='radio' name='radioIsRoll' id='radioisTure' value='True' checked='checked' /><lable>是</lable><input type='radio' name='radioIsRoll' id='radioisfalse' value='False'/><lable>否</lable></td></tr></table>"

    }

    $.dialog({
        title: "日志设置模板",
        content: content,
        button: [
                    {
                        name: "提交",
                        callback: function () {
                            AddMould(lbl, $("#txtName"), Id);
                        }
                    },
                    {
                        name: "取消",
                        callback: function () {
                            this.close();
                        }
                    }
                    ],
        lock: true,
        drag: false
    });
}
function AddMould(contr, sortNum, sortId) {
    var Id = sortId;
    var Name = $(sortNum).parent().parent().parent().find("#txtName").val();
    var IsFullScreen = $(sortNum).parent().parent().parent().find("input[name=radioIsFullScreen]:checked").val();
    var IsStick = $(sortNum).parent().parent().parent().find("input[name=radioIsStick]:checked").val();
    var IsRoll = $(sortNum).parent().parent().parent().find("input[name=radioIsRoll]:checked").val();
    var data = contr == null ? { action: "Add", Name: Name, IsFullScreen: IsFullScreen, IsStick: IsStick, IsRoll: IsRoll} :
              { action: "Update", id: Id, Name: Name, IsFullScreen: IsFullScreen, IsStick: IsStick, IsRoll: IsRoll };
    $.ajax({
        type: "post",
        data: data,
        async: true,
        url: "../../Ajax/NoticeTypeAjax.ashx",
        beforeSend: function () {
        },
        success: function (data) {
            $.dialog.tips("更新成功！", 1);
            if (contr == null) {
                var tbody = "<tr>";
                tbody += "<td><lable name='lblName' id='" + data + "'>";
                tbody += Name;
                tbody += "</lable></td>";
                tbody += "<td><lable name='lblIsFullScreen'>";
                tbody += IsFullScreen;
                tbody += "</lable></td>";
                tbody += "<td><lable name='lblIsStick'>";
                tbody += IsStick;
                tbody += "</lable></td>";
                tbody += "<td><lable name='lblIsRoll'>";
                tbody += IsRoll;
                tbody += "</lable></td>";
                tbody += "<td>";
                tbody += "<input type='button' onclick='Update(this)' value='更新'><input type='button' onclick='Delete(this)' value='删除'>"
                tbody += "</td>";
                tbody += "</tr>"
                $("#tb1").append(tbody);
            }
            else {
                $(contr).parent().parent().find("lable[name=lblName]").first().text(Name);
                $(contr).parent().parent().find("lable[name=lblIsFullScreen]").first().text(IsFullScreen);
                $(contr).parent().parent().find("lable[name=lblIsStick]").first().text(IsStick);
                $(contr).parent().parent().find("lable[name=lblIsRoll]").first().text(IsRoll);
            }
        },
        complete: function () {
        },
        error: function () {
        }
    })
}
function Delete(contr) {
    var id = $(contr).parent().parent().find("lable[name=lblName]").first().attr("id");
    if (window.confirm("确认删除？")) {
        $.ajax({
            type: "post",
            data: { action: "Delete", id: id },
            async: true,
            url: "../../Ajax/NoticeTypeAjax.ashx",
            beforeSend: function () {
            },
            success: function (data) {
                if (data == "True") {
                    $(contr).parent().parent().remove();
                    $.dialog.tips("删除成功！", 1);
                }
                else {
                    $.dialog.tips("删除失败！", 1);
                }
            },
            complete: function () {
            },
            error: function () {
            }
        })
    }
}
